package dao;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import model.complaintModel;
import model.complaint_TypeModel;
import util.dataUtil;

public class complaint_typeDao {
	public static int updateComplaint(complaint_TypeModel model) {
		int result = 0;
		try {

			
			CallableStatement csmt = dataUtil.getconnConnection().prepareCall("{call sp_UpdateComplaint_type(?,?,?,?)}");
			
		
			csmt.setInt("ID", model.getId());
			csmt.setString("name", model.getName());
			csmt.setString("Last_updated", model.getLast_updated());
			csmt.registerOutParameter("RESULT", java.sql.Types.INTEGER);

			csmt.execute();

			
			result = csmt.getInt("RESULT");
		} catch (SQLException e) {
		result = 0;
		}
		return result;
		}
	//delete complaint
	public static int deleteUser(complaint_TypeModel model) {
		int result = 0;
		try {
			CallableStatement csmt = dataUtil.getconnConnection().prepareCall("{call sp_DeleteComplaint_type(?,?)}");
			csmt.setInt("ID", model.getId());
			csmt.registerOutParameter("RESULT", java.sql.Types.INTEGER);
			csmt.execute();
			result = csmt.getInt("RESULT");
		} catch (SQLException e) {
		result = 0;
		}
		return result;
			
		}
	//add user
	public static int InsertComplaint(complaint_TypeModel model) {
		int result = 0;
		try {

			
			CallableStatement csmt = dataUtil.getconnConnection().prepareCall("{call sp_InsertComplaint_Type(?,?,?)}");
			csmt.setString("name", model.getName());
			csmt.setString("Last_updated", model.getLast_updated());
			csmt.registerOutParameter("RESULT", java.sql.Types.INTEGER);

			csmt.execute();

			
			result = csmt.getInt("RESULT");
		} catch (SQLException e) {
		result = 0;
		}
		return result;
		}
	public static ArrayList getAllComplaint_type() {
		ArrayList listComplaint = new ArrayList();
		String sql = "SELECT * FROM Complaint_type";
		try {
			PreparedStatement ps = dataUtil.getconnConnection().prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while(rs.next()) {
				complaint_TypeModel model = new complaint_TypeModel();
				//model.setId(rs.getInt("ID"));
				model.setName(rs.getString("name"));
				//model.setLast_updated(rs.getString("last_Updated"));
				listComplaint.add(model);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return listComplaint;
	}
}
